import router from '@system.router';
import prompt from '@system.prompt';

export default {
    data: {
        isLocal : false,
        localresult : -1,
        remoteResult : -1,
        win_count : 0,
        lost_count : 0,
        equal_count : 0,
        play_count : 0,
        play_result_desc : "",

        tempImaPath : "/common/wait.jpg",
        imgpathright: "/common/wait.jpg",
        imgpathleft : "/common/wait.jpg"
    },

    imageClickFun(inputValue,e){
        // 当前点击为标记为我的
        this.isLocal = true;
        this.changeShow(inputValue)
        // 此处注意JSON字符串格式
        this.sendToRemote('{"result":"'+ inputValue +'","is_local":'+ false +'}')
    },

    connectCallBack(data){
        //收到对方设备数据
        let msg = JSON.parse(data).data;
        if(msg.is_local != undefined){
            this.isLocal = msg.is_local;
        }
        if(msg.result != undefined){
            this.changeShow(msg.result)
        }
        if(msg.restart != undefined){
            this.reStart()
        }
    },

    changeShow(res){
        if(this.isLocal){
            this.localresult = res
            switch(res){
                case '0':
                    this.imgpathright = "/common/shitou.jpg";
                    break;
                case '1':
                    this.imgpathright = "/common/jiandao.jpg";
                    break;
                case '2':
                    this.imgpathright = "/common/bu.jpg";
                    break;
            }
        }else{
            this.remoteResult = res
            switch(res){
                case '0':
                    this.imgpathleft = "/common/shitou.jpg";
                    break;
                case '1':
                    this.imgpathleft = "/common/jiandao.jpg";
                    break;
                case '2':
                    this.imgpathleft = "/common/bu.jpg";
                    break;
            }
        }

        if(this.localresult >= 0 && this.remoteResult >= 0){
            this.play_count ++ ;
            var calc1 = parseInt(this.localresult);
            var calc2 = parseInt(this.remoteResult);
            var calcRes= calc2 - calc1;
            if(calcRes ==0){
                // 平局
                this.equal_count ++;
                this.play_result_desc = "平局！";
            }else if(calcRes==1||calcRes==-2){
                this.win_count ++;
                this.play_result_desc = "赢啦！真厉害！";
            }else{
                this.lost_count ++;
                this.play_result_desc = "没关系！再来一局！";
            }
        }
    },

    sendToRemote(msg){
        //通知service向对方设备发送信息
        FeatureAbility.callAbility({
            bundleName: 'com.myharos.fingerguessing',
            abilityName: 'com.myharos.fingerguessing.connect',
            messageCode: 3,
            data: msg,
            abilityType: 0,
            syncOption: 0
        })
    },

    async onShow(){
        //订阅service用于接收对方设备的数据，同时发送数据给对方
        let res = await FeatureAbility.subscribeAbilityEvent({
            bundleName: 'com.myharos.fingerguessing',
            abilityName: 'com.myharos.fingerguessing.connect',
            messageCode: 1,
            abilityType: 0,
            syncOption: 0
        }, this.connectCallBack)
        if(res == 'true'){
            //订阅成功，开始
            setTimeout(()=>{
                this.start()
            }, 200)
            // prompt.showToast({message:'开始'})
        }else{
            prompt.showToast({message:'连接失败'})
        }
        this.start()
    },

    start(){

    }

}
